package com.atguigu.gmall.seckill.task;/*
 * @author: XueYouPeng
 * @time: 23.8.2 下午 7:37
 */

import com.atguigu.gmall.seckill.entity.SeckillGoods;
import com.atguigu.gmall.seckill.service.SeckillGoodsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.concurrent.ThreadPoolExecutor;

@Component
@Slf4j
public class PreCacheHotTask {

    @Autowired
    private SeckillGoodsService seckillGoodsService;

    @Autowired
    private ThreadPoolExecutor threadPoolExecutor;

    @Scheduled(cron = "0 0 2 * * ?")
    public void cacheHot(){

        // 从数据库中查询出满足条件的秒杀商品数据
        List<SeckillGoods> seckillGoodsList = seckillGoodsService.findAllSeckillGoods();

        // 把秒杀商品数据存储到Redis中
        threadPoolExecutor.submit(() -> {
            seckillGoodsService.saveSseckillGoodsToRedis(seckillGoodsList);
            log.info("数据已经被缓存到了Redis中...");
        });

        // 把秒杀商品数据存储到本地缓存中
        threadPoolExecutor.submit(() -> {
            seckillGoodsService.saveSseckillGoodsToLocalCache(seckillGoodsList);
            log.info("数据已经被缓存到了本地缓存中...");
        });
    }
}
